package com.xizi.forum.service.impl;

import com.xizi.forum.common.ResultCode;
import com.xizi.forum.dao.ArticleInfoMapper;
import com.xizi.forum.dao.ArticleReplyInfoMapper;
import com.xizi.forum.exception.ApplicationException;
import com.xizi.forum.model.ArticleInfo;
import com.xizi.forum.model.ArticleReplyInfo;
import com.xizi.forum.service.IArticleReplyInfoService;
import com.xizi.forum.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ArticleReplyInfoServiceImpl implements IArticleReplyInfoService {

    @Autowired
    private ArticleReplyInfoMapper articleReplyInfoMapper;

    @Autowired
    private ArticleInfoMapper articleInfoMapper;

    /**
     * 新建回复帖子
     * @param articleId 帖子作者id
     * @param content 回复帖子内容
     * @param userId 发送回复帖子Id
     * @return
     */
    @Override
    public Boolean create(Long articleId, String content,Long userId) {
        //1.参数校验
        if(articleId == null || articleId < 0 || StringUtil.isEmpty(content)){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        //2.校验当前帖子状态
        ArticleInfo articleInfo = articleInfoMapper.selectByPrimaryKey(articleId);
        if(articleInfo == null || articleInfo.getDeleteState() == 1){
            log.error(ResultCode.FAILED_FORBIDDEN.toString() + "当前帖子不能被访问到 articleId ： " + articleId);
            throw  new ApplicationException(ResultCode.FAILED_FORBIDDEN);
        }
        //1. 修改帖子的回复数
        ArticleInfo updateArticleInfo = new ArticleInfo();
        updateArticleInfo.setId(articleId);
        updateArticleInfo.setReplyCount(articleInfo.getReplyCount() + 1);
        int row1 = articleInfoMapper.updateByPrimaryKeySelective(updateArticleInfo);
        log.info("修改帖子的回复数 : {}",row1);

        //2.添加回复帖子
        ArticleReplyInfo articleReplyInfo = new ArticleReplyInfo();
        articleReplyInfo.setArticleId(articleId);
        articleReplyInfo.setContent(content);
        articleReplyInfo.setPostUserId(userId);
        int row2 = articleReplyInfoMapper.insertSelective(articleReplyInfo);
        log.info("添加帖子的回复内容数 : {}",row2);

        //3.校验sql状况
        if(row1 != 1 || row2 != 1){
            log.error(ResultCode.FAILED_SERVICE.toString());
            throw new ApplicationException(ResultCode.FAILED_SERVICE);
        }
        return true;
    }

    /**
     * 获取当前帖子下的所用回复帖子
     * @param articleId 当前帖子作者id
     * @return
     */
    @Override
    public List<ArticleReplyInfo> selectAllRepliesByArticleId(Long articleId) {
        //1.参数校验
        if(articleId == null || articleId <= 0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        List<ArticleReplyInfo> list = articleReplyInfoMapper.selectAllRepliesByArticleId(articleId);
        log.info("回复帖子 list : {}",list);
        return list;
    }

    /**
     * 通过id查询回复帖子
     * @param id
     * @return
     */
    @Override
    public ArticleReplyInfo selectArticleReplyById(Long id) {
        // 1.参数校验
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        ArticleReplyInfo articleReplyInfo = articleReplyInfoMapper.selectByPrimaryKey(id);
        return articleReplyInfo;
    }

    /**
     * 删除回复帖子（普通用户只能删除自己的）
     * @param articleReplyId 回复帖子id
     * @param articleId 帖子id
     * @return
     */
    @Override
    public void deleteArticleReplyById(Long articleReplyId, Long articleId) {
        // 1.参数校验
        if(articleReplyId == null || articleReplyId <= 0 || articleId == null || articleId <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        //2.改变回复帖子状态
        ArticleReplyInfo updateArticleReplyInfo = new ArticleReplyInfo();
        updateArticleReplyInfo.setId(articleReplyId);
        updateArticleReplyInfo.setDeleteState((byte) 1);
        int row1 = articleReplyInfoMapper.updateByPrimaryKeySelective(updateArticleReplyInfo);

        //3.改变对应帖子下的回复数量
        ArticleInfo articleInfo = articleInfoMapper.selectByPrimaryKey(articleId);
        if(articleInfo == null || articleInfo.getDeleteState() == 1){
            log.error(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS);
        }
        ArticleInfo updateArticleInfo = new ArticleInfo();
        updateArticleInfo.setId(articleId);
        updateArticleInfo.setReplyCount(articleInfo.getReplyCount() + 1);
        int row2 = articleInfoMapper.updateByPrimaryKeySelective(updateArticleInfo);


        if(row1 != 1 || row2 != 1){
            log.error("sql 异常，删除回复帖子失败！");
            throw new ApplicationException(ResultCode.FAILED_SERVICE);
        }
    }


}
